package com.ying.tests.mongo.service.impl;

import com.ying.common.entity.Response;
import com.ying.tests.mongo.dao.StudentDao;
import com.ying.tests.mongo.entity.Student;
import com.ying.tests.mongo.service.MongoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class MongoServiceImpl implements MongoService {

    private static final Logger logger = LoggerFactory.getLogger(MongoServiceImpl.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StudentDao studentDao;

    @Override
    public Response insert(Student student) {
        student.builder()
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        studentDao.save(student);
        return new Response().success();
    }

    @Override
    public Response select(Student student) {
        List<Student> result = studentDao.findByName(student.getName());
        return null != result ? new Response().success("查询成功") : new Response().failure("查询失败");
    }
    
}
